package red.mlz.module.config;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DruidMonitorConfig {

    /**
     * 注册 Druid 监控界面 Servlet
     */
    @Bean
    public ServletRegistrationBean<StatViewServlet> druidStatViewServlet() {
        ServletRegistrationBean<StatViewServlet> registrationBean = new ServletRegistrationBean<>(
                new StatViewServlet(), "/druid/*");

        // 设置初始化参数
        registrationBean.addInitParameter("loginUsername", "admin");
        registrationBean.addInitParameter("loginPassword", "123456");
        registrationBean.addInitParameter("resetEnable", "true");
        registrationBean.addInitParameter("allow", "127.0.0.1");
        registrationBean.addInitParameter("deny", "");

        return registrationBean;
    }

    /**
     * 注册 Druid 监控过滤器
     */
    @Bean
    public FilterRegistrationBean<WebStatFilter> druidWebStatFilter() {
        FilterRegistrationBean<WebStatFilter> registrationBean = new FilterRegistrationBean<>(
                new WebStatFilter());

        // 设置初始化参数
        registrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        registrationBean.addInitParameter("profileEnable", "true");
        registrationBean.addInitParameter("principalSessionName", "admin");
        registrationBean.addInitParameter("principalCookieName", "admin");
        registrationBean.addInitParameter("sessionStatEnable", "true");
        registrationBean.addInitParameter("sessionStatMaxCount", "100000");

        // 添加 URL 模式
        registrationBean.addUrlPatterns("/*");

        return registrationBean;
    }
}